/**
 * 本地缓存(库)
 * @param i
 * @constructor
 */
function LeopardStorage(i) {
    this.idx = 'leopard.store.' + i;
};
/**
 * 唯一ID
 * @returns {string}
 */
LeopardStorage.prototype.uid = function() {
    var u = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
    return u.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = (c == 'x') ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};
/**
 * 在表(key)中添加一项数据
 * @param k
 * @param v
 * @returns {string}
 */
LeopardStorage.prototype.add = function(k, v) {
    if (k && v) {
        var _d = localStorage.getItem(this.idx + '.' + k), u = this.uid();
        _d = (_d || '') + '&' + u + '#' + v.encode();
        localStorage.setItem(this.idx + '.' + k, _d);
        return u;
    } else {
        throw 'LeopardStorage.add: key or val is undefined';
    }
};
/**
 * 设置表(key)的数据
 * @param k
 * @param v
 * @returns {string}
 */
LeopardStorage.prototype.set = function(k, v) {
    if (k && v) {
        var u = this.uid();
        localStorage.setItem(this.idx + '.' + k, '&' + u + '#' + v.encode());
        return u;
    } else {
        throw 'LeopardStorage.set: key or val is undefined';
    }
};
/**
 * 获取表中的所有数据
 * @param k
 * @returns {*}
 */
LeopardStorage.prototype.get = function(k) {
    if (k) {
        var d = localStorage.getItem(this.idx + '.' + k);
        if (d) {
            var l = d.split('&');
            if (l) {
                var r = new Array();
                for (var j = 1; j < l.length; j ++) {
                    var i = l[j].split('#')[0], v = l[j].split('#')[1];
                    r.push({ 'index' : i, 'value' : v.decode() });
                }
                return r;
            }
        }
        return null;
    } else {
        throw 'LeopardStorage.get: key is undefined';
    }
};
/**
 * 根据唯一ID查询表(key)中的一项数据
 * @param k
 * @param u
 * @returns {*}
 */
LeopardStorage.prototype.sel = function(k, u) {
    if (k && u) {
        var d = localStorage.getItem(this.idx + '.' + k);
        if (d) {
            d = d.match(new RegExp('&' + u + '#[^&#]+', 'g'));
            if (d) {
                d = d[0].replace(/^&/, '');
                var i = d.split('#')[0], v = d.split('#')[1];
                return { 'index' : i, 'value' : v.decode() };
            } else {
                return null;
            }
        }
    } else {
        throw 'LeopardStorage.del: key or uid is undefined';
    }
};
/**
 * 根据唯一ID删除表(key)中的一项数据
 * @param k
 * @param u
 */
LeopardStorage.prototype.del = function(k, u) {
    if (k && u) {
        var d = localStorage.getItem(this.idx + '.' + k);
        if (d) {
            d = d.replace(new RegExp('&' + u + '#[^&#]+', 'g'), '');
            localStorage.setItem(this.idx + '.' + k, d);
        }
    } else {
        throw 'LeopardStorage.del: key or uid is undefined';
    }
};
/**
 * 删除表(key)中的所有数据
 * @param k
 */
LeopardStorage.prototype.rmv = function(k) {
    if (k) {
        localStorage.removeItem(this.idx + '.' + k);
    } else {
        throw 'LeopardStorage.rmv: key is undefined';
    }
};
/**
 * 清除整个库(idx)
 */
LeopardStorage.prototype.clr = function() {
    var l = localStorage.length, a = [], i = 0;
    for (; l && i < l; i ++) {
        var k = localStorage.key(i);
        k.startsWith(this.idx) ? a.push(k) : 0;
    }
    for (i = 0; i < a.length; i ++) {
        localStorage.removeItem(a[i]);
    }
};